{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输出混淆矩阵、精准率、召回率\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import classification_report, confusion_matrix\n",
    "test_dir = \"/kaggle/input/tomatolay/normal/test\"\n",
    "test_dir_samples = get_nb_files(test_dir)\n",
    "test_generator = test_datagen.flow_from_directory(test_dir,\n",
    "                                                  target_size=(IM_WIDTH,\n",
    "                                                               IM_HEIGHT),\n",
    "                                                  batch_size=batch_size,\n",
    "                                                  class_mode='categorical',\n",
    "                                                  shuffle=False)\n",
    "Y_pred = model.predict_generator(test_generator,\n",
    "                                 test_dir_samples // batch_size + 1)\n",
    "y_pred = np.argmax(Y_pred, axis=1)\n",
    "print('Confusion Matrix')\n",
    "print(confusion_matrix(test_generator.classes, y_pred))\n",
    "print('Classification Report')\n",
    "target_names = li\n",
    "print(\n",
    "    classification_report(test_generator.classes,\n",
    "                          y_pred,\n",
    "                          target_names=target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制混淆矩阵\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "import itertools\n",
    "\n",
    "\n",
    "def plot_confusion_matrix(cm,\n",
    "                          classes,\n",
    "                          normalize=False,\n",
    "                          title='Confusion matrix',\n",
    "                          cmap=plt.cm.Blues):\n",
    "    \"\"\"\n",
    "    This function prints and plots the confusion matrix.\n",
    "    Normalization can be applied by setting `normalize=True`.\n",
    "    Input\n",
    "    - cm : 计算出的混淆矩阵的值\n",
    "    - classes : 混淆矩阵中每一行每一列对应的列\n",
    "    - normalize : True:显示百分比, False:显示个数\n",
    "    \"\"\"\n",
    "\n",
    "    print(cm)\n",
    "    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n",
    "    plt.title(title)\n",
    "    plt.colorbar()\n",
    "    tick_marks = np.arange(len(classes))\n",
    "    plt.xticks(tick_marks, classes, rotation=45)\n",
    "    plt.yticks(tick_marks, classes)\n",
    "    fmt = '.2f' if normalize else 'd'\n",
    "    thresh = cm.max() / 2.\n",
    "    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n",
    "        plt.text(j,\n",
    "                 i,\n",
    "                 format(cm[i, j], fmt),\n",
    "                 horizontalalignment=\"center\",\n",
    "                 color=\"white\" if cm[i, j] > thresh else \"black\")\n",
    "    plt.tight_layout()\n",
    "\n",
    "\n",
    "#     plt.ylabel('真实标签')\n",
    "#     plt.xlabel('预测标签')\n",
    "import numpy as np\n",
    "cnf_matrix = np.array([[391, 2, 16, 1], [2, 475, 0, 0], [24, 0, 453, 0],\n",
    "                       [0, 0, 0, 465]])\n",
    "attack_types = ['灰斑病', '普通锈病', '大斑病', '健康']  #分类标签\n",
    "plot_confusion_matrix(cnf_matrix,\n",
    "                      classes=attack_types,\n",
    "                      normalize=True,\n",
    "                      title='混淆矩阵')\n",
    "plt.savefig('1.pdf')  #保存为pdf高清"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# matplotlib绘制acc折线图\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams['font.sans-serif'] = ['STSong']  # 用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号\n",
    "epoch = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n",
    "\n",
    "xception_acc = [\n",
    "    0.276017427444458, 0.6281976699829102, 0.7881540656089783, 0.8494912981987,\n",
    "    0.8834302425384521, 0.9023255705833435, 0.914607584476471,\n",
    "    0.9272528886795044, 0.9354650974273682, 0.9425871968269348,\n",
    "    0.9468749761581421, 0.9530523419380188, 0.9556686282157898,\n",
    "    0.9602470993995667, 0.9608284831047058, 0.9653342962265015,\n",
    "    0.9650436043739319, 0.9678052067756653, 0.9698401093482971,\n",
    "    0.9718023538589478\n",
    "]\n",
    "VGG16_acc = [\n",
    "    0.7160610556602478, 0.9191860556602478, 0.9449854493141174,\n",
    "    0.9573401212692261, 0.9686046242713928, 0.9755814075469971,\n",
    "    0.9763081669807434, 0.9802325367927551, 0.9827035069465637,\n",
    "    0.9859738349914551, 0.986482560634613, 0.9880087375640869,\n",
    "    0.9897528886795044, 0.9906976819038391, 0.992223858833313,\n",
    "    0.9910610318183899, 0.9922965168952942, 0.9930959343910217,\n",
    "    0.9936773180961609, 0.9936046600341797\n",
    "]\n",
    "\n",
    "MobileNetv2_acc = [\n",
    "    0.7429505586624146, 0.9149709343910217, 0.942732572555542,\n",
    "    0.9542877674102783, 0.9622092843055725, 0.9684593081474304,\n",
    "    0.9747819900512695, 0.9744912981987, 0.9807412624359131,\n",
    "    0.9816133975982666, 0.9831395149230957, 0.9845930337905884,\n",
    "    0.9851017594337463, 0.9854651093482971, 0.986991286277771,\n",
    "    0.9897528886795044, 0.9897528886795044, 0.9899709224700928,\n",
    "    0.9912064075469971, 0.9922965168952942\n",
    "]\n",
    "\n",
    "Inceptionv3_acc = [\n",
    "    0.41213661432266235, 0.7633720636367798, 0.8493459224700928,\n",
    "    0.8960028886795044, 0.9129360318183899, 0.930159866809845,\n",
    "    0.936991274356842, 0.9485465288162231, 0.9506540894508362,\n",
    "    0.9564680457115173, 0.9623546600341797, 0.9645348787307739,\n",
    "    0.965624988079071, 0.9694767594337463, 0.9741278886795044,\n",
    "    0.9750726819038391, 0.9748546481132507, 0.9781249761581421,\n",
    "    0.977034866809845, 0.9790697693824768\n",
    "]\n",
    "\n",
    "ResNet50_acc = [\n",
    "    0.7877907156944275, 0.9468023180961609, 0.9649709463119507,\n",
    "    0.9742006063461304, 0.9800872206687927, 0.9836482405662537,\n",
    "    0.9850290417671204, 0.9889534711837769, 0.9913517236709595,\n",
    "    0.9918604493141174, 0.9932412505149841, 0.9937499761581421,\n",
    "    0.9925145506858826, 0.9925145506858826, 0.9941860437393188,\n",
    "    0.994767427444458, 0.9944767355918884, 0.9949854612350464,\n",
    "    0.9963662624359131, 0.9962936043739319\n",
    "]\n",
    "\n",
    "gj_acc = [\n",
    "    0.84258723, 0.96497095, 0.9789971, 0.9835029, 0.98953485, 0.98909885,\n",
    "    0.99106103, 0.9928779, 0.9952035, 0.9949128, 0.99527615, 0.9962936,\n",
    "    0.997093, 0.99636626, 0.9961482, 0.99651164, 0.9976744, 0.9980378,\n",
    "    0.99694765, 0.9983285\n",
    "]\n",
    "\n",
    "plt.plot(epoch, gj_acc, 'c-^', label=\"C-ResNeXt\")\n",
    "plt.plot(epoch, ResNet50_acc, 'y-o', label=\"ResNet101\")\n",
    "plt.plot(epoch, MobileNetv2_acc, 'm-s', label=\"MobileNetV2\")\n",
    "plt.plot(epoch, Inceptionv3_acc, 'r-*', label=\"InceptionV3\")\n",
    "plt.plot(epoch, VGG16_acc, 'b-p', label=\"VGG16\")\n",
    "plt.plot(epoch, xception_acc, 'g-<', label=\"Xception\")\n",
    "\n",
    "plt.legend()\n",
    "plt.ylabel('Accuracy 准确率')\n",
    "plt.xlabel('Epoch 轮次')\n",
    "plt.title('Training Accuracy 训练准确率')\n",
    "plt.savefig('D:\\\\acc.pdf')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf-gpu",
   "language": "python",
   "name": "tf-gpu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
